﻿using Sgr.Domain.Repositories;

namespace Sgr.EventBus.RelayBoxs.Domain
{
    public interface IOutboxMessageRepository : IBaseRepositoryOfTEntityAndTPrimaryKey<OutboxMessage, long>
    {
        /// <summary>
        /// 获取待发送的消息列表
        /// </summary>
        /// <param name="limit">取值范围 0 -2000 </param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        Task<IEnumerable<OutboxMessage>> GetPendingMessagesAsync(int limit, CancellationToken cancellationToken = default);

        /// <summary>
        /// 标记为已发送
        /// </summary>
        Task MarkDispatchedAsync(IEnumerable<long> messageIds, CancellationToken cancellationToken = default);

        /// <summary>
        /// 标记为已发送
        /// </summary>
        Task MarkDispatchedAsync(long messageId, CancellationToken cancellationToken = default);

        /// <summary>
        /// 标记为发送失败
        /// </summary>
        Task MarkFailedAsync(long messageId, string lastError = "", CancellationToken cancellationToken = default);

        /// <summary>
        /// 增加重试计数（Dispatcher 用）
        /// </summary>
        Task MarkRetryAsync(long messageId, DateTime nextOnUtc, string lastError = "", CancellationToken cancellationToken = default);

        /// <summary>
        /// 清理已成功投递且超过保留期限的消息。
        /// </summary>
        /// <param name="olderThanUtc">早于该时间点的成功消息将被清理</param>
        /// <param name="max">单次清理的记录最大条数</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>实际清理的条数</returns>
        Task<int> CleanupDispatchedMessagesAsync(DateTime olderThanUtc, int max, CancellationToken cancellationToken = default);

        /// <summary>
        /// 清理已失败且超过最大重试次数（或失败时间过久）的消息。
        /// </summary>
        /// <param name="olderThanUtc">早于该时间点的失败消息将被清理</param>
        /// <param name="max">单次清理的记录最大条数</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns>实际清理的条数</returns>
        Task<int> CleanupFailedMessagesAsync(DateTime olderThanUtc, int max, CancellationToken cancellationToken = default);
    }
}